package com.junsi.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Condition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Client;
import com.junsi.bean.Department;
import com.junsi.bean.Operate;
import com.junsi.bean.Rank;
import com.junsi.bean.Record;
import com.junsi.bean.Staff;
import com.junsi.bean.Usertracking;
import com.junsi.service.OperateService;
import com.junsi.service.RankService;
import com.junsi.service.RecordService;
import com.junsi.service.StaffSerivce;
import com.junsi.service.UsertrackingService;
import com.junsi.util.FileUtil;
import com.junsi.util.InteractiveUtil;
import com.junsi.util.PageUtil;
import com.junsi.util.UsertrackUtil;

/**
 * 用户跟踪控制层
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/Operate")
public class OperateController {

	@Autowired
	private StaffSerivce staffSerivce;
	@Autowired
	private OperateService operateService;

	Logger logger = Logger.getRootLogger();

	/**
	 * 运营总页数,页码未审批
	 * 
	 * @param staff
	 * @param file
	 * @return
	 */
	@RequestMapping("/count")
	public ModelAndView count(PageUtil page, HttpServletResponse response, HttpServletRequest request,
			String condition) {
		ModelAndView modelAndView = new ModelAndView();
		String idString = (String) request.getSession().getAttribute("staffs");
		String[] id = idString.split(",");
		page.setPageSize(10);
		int count;
		if (condition != null && !"".equals(condition)) {
			count = operateService.findCountConditionTwo(condition.trim());
			page.setRowCount(count);
			modelAndView.addObject("condition", condition);
		} else {
			count = operateService.findCount(id);
			page.setRowCount(count);
		}
		modelAndView.addObject("PageCount", page.getPageCount());
		modelAndView.addObject("PageNum", page.getPageNum());
		modelAndView.setViewName("forward:/salejihui/html/operation.jsp");
		return modelAndView;
	}

	/**
	 * 运营总页数,页码未审批
	 * 
	 * @param staff
	 * @param file
	 * @return
	 */
	@RequestMapping("/count1")
	public ModelAndView count1(PageUtil page, HttpServletResponse response, HttpServletRequest request, String select,
			String leibie, Integer bianhao, String condition) {
		ModelAndView modelAndView = new ModelAndView();
		String idString = (String) request.getSession().getAttribute("staffs");
		String[] id = idString.split(",");
		page.setPageSize(10);
		int count;
		if (condition != null && !"".equals(condition)) {
			count = operateService.findCountCondition(condition);
			modelAndView.addObject("condition", condition);
		} else {
			count = operateService.findCount(id);
		}
		page.setRowCount(count);
		modelAndView.addObject("PageCount", page.getPageCount());
		modelAndView.addObject("PageNum", page.getPageNum());

		modelAndView.addObject("select", select);
		modelAndView.addObject("bianhao", bianhao);
		modelAndView.addObject("leibie", leibie);
		modelAndView.setViewName("forward:/client/html/yunyingguanli.jsp");
		return modelAndView;
	}

	/**
	 * 分页查询所有用户跟踪信息
	 * 
	 * @param
	 */
	@RequestMapping("/findoperate")
	public void pageFindSale(PageUtil page, HttpServletResponse response, HttpServletRequest request,
			String condition) {
		// 获取员工ID
		String idString = (String) request.getSession().getAttribute("staffs");
		String[] id = idString.split(",");
		page.setPageSize(5);
		if (idString != null) {
			Integer count = null;
			if (condition != null && !"".equals(condition)) {
				count = operateService.findCountConditionTwo(condition.trim());
				page.setRowCount(count);
			} else {
				count = operateService.findCount(id);
				page.setRowCount(count);
			}
			List<Operate> operates = null;
			if (count != null) {
				// 根据员工ID来查找销售机会
				if (condition != null && !"".equals(condition)) {
					operates = operateService.findByNameTwo(condition.trim(), page);
				} else {
					operates = operateService.pageFind(id, page.getEndIndex(), page.getBeginIndex());
				}
				String role = (String) request.getSession().getAttribute("role");
				if (role == null) {
					for (Operate operate : operates) {
						operate.setoPassword("无权查看");
					}
				}
			}
			response.setContentType("text/json; charset=UTF-8");
			InteractiveUtil.WriteJsonForPage(operates, page, response);
		}
	}

	/**
	 * 后台分页查询所有用户跟踪信息
	 * 
	 * @param
	 */
	@RequestMapping("/findhoutaioperate")
	public void findhoutaioperate(PageUtil page, String name, HttpServletResponse response, HttpServletRequest request,
			String condition) {
		if (!"".equals(name) && name != null) {
			// 获取员工ID
			String idString = (String) request.getSession().getAttribute("staffs");
			String[] id = idString.split(",");
			page.setPageSize(5);
			if (idString != null) {
				Integer count = null;
				if (condition != null && !"".equals(condition)) {
					count = operateService.findCountCondition(condition);
				} else {
					count = operateService.findCount(id);
				}
				List<Operate> operates = null;
				if (count != null) {
					// 根据员工ID来查找销售机会
					if (condition != null && !"".equals(condition)) {
						page.setRowCount(count);
						operates = operateService.findByhoutaiNameCondition(condition, page);
					} else {
						page.setRowCount(count);
						operates = operateService.findByhoutaiName(name, page.getEndIndex(), page.getBeginIndex());
					}
					String role = (String) request.getSession().getAttribute("role");
					if (role == null) {
						for (Operate operate : operates) {
							operate.setoPassword("无权查看");
						}
					}
				}
				response.setContentType("text/json; charset=UTF-8");
				InteractiveUtil.WriteJsonForPage(operates, page, response);
			}
		} else {
			// 获取员工ID
			String idString = (String) request.getSession().getAttribute("staffs");
			String[] id = idString.split(",");
			page.setPageSize(5);
			if (idString != null) {
				Integer count = null;
				if (condition != null && !"".equals(condition)) {
					count = operateService.findCountCondition(condition);
				} else {
					count = operateService.findCount(id);
				}
				List<Operate> operates = null;
				if (count != null) {
					page.setRowCount(count);
					// 根据员工ID来查找销售机会
					if (condition != null && !"".equals(condition)) {
						page.setRowCount(count);
						operates = operateService.findByhoutaiNameCondition(condition, page);
					} else {
						page.setRowCount(count);
						operates = operateService.pageFind(id, page.getEndIndex(), page.getBeginIndex());
					}
					String role = (String) request.getSession().getAttribute("role");
					if (role == null) {
						for (Operate operate : operates) {
							operate.setoPassword("无权查看");
						}
					}
				}
				response.setContentType("text/json; charset=UTF-8");
				InteractiveUtil.WriteJsonForPage(operates, page, response);
			}
		}

	}

	/**
	 * 用户跟踪新增
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/insertoperate")
	@ResponseBody
	public String insertoperate(HttpServletResponse response, Operate operate, HttpServletRequest request)
			throws Exception {
		if (operate != null) {
			operate.setOcreatTime(new Date());
			Boolean result = operateService.insert(operate);
			if (result) {
				return "success";
			} else {
				return "error";
			}
		}
		return "error";
	}

	/**
	 * 根据ID来查询对象
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/findidoperate")
	public void findidoperate(HttpServletResponse response, Integer id, HttpServletRequest request) throws Exception {
		Operate operate = null;
		if (id != null) {
			operate = operateService.findById(id);
			if (operate != null) {
				InteractiveUtil.WriteBeanForHtml(operate, response);
			} else {
				InteractiveUtil.WriteStringForHtml("数据不存在，请刷新重试", response);
			}
		}
	}

	/**
	 * 销售机会编辑
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/updateoperate")
	@ResponseBody
	public void updateSale(HttpServletResponse response, Operate operate, HttpServletRequest request) throws Exception {
		if (operate != null) {
			Boolean reuslt = operateService.update(operate);
			if (reuslt) {
				InteractiveUtil.WriteStringForHtml("编辑成功", response);
			} else {
				InteractiveUtil.WriteStringForHtml("编辑失败", response);
			}
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteManyId")
	public void deleteManyId(String delid, HttpServletResponse response) {
		boolean reuslt = false;
		if (delid != null) {
			String[] delid1 = delid.split(",");
			for (String string : delid1) {
				Integer del = Integer.parseInt(string);
				reuslt = operateService.delete(del);
			}
			if (reuslt) {
				InteractiveUtil.WriteStringForHtml("删除成功", response);
			} else {
				InteractiveUtil.WriteStringForHtml("删除失败", response);
			}

		}
		InteractiveUtil.WriteStringForHtml("删除失败", response);
	}

}
